//
//  WJNavigationController.m
//  BeDeJie
//
//  Created by jerry on 2017/1/25.
//  Copyright © 2017年 jerry. All rights reserved.
//

#import "WJNavigationController.h"

@interface WJNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation WJNavigationController

+(void)load {
    //获取哪个类下的UINavigationBar
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    
    //设置导航条标题
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
    [navBar setTitleTextAttributes:attrs];
    
    //设置导航条背景图片
    [navBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg_white"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //滑动关闭页面
    [self backWithGesture];

}

//全屏滑动返回
- (void)backWithGesture {
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    [self.view addGestureRecognizer:panGestureRecognizer];
    
    // 控制手势什么时候触发,只有非根控制器才需要触发手势
    panGestureRecognizer.delegate = self;
    // 禁止之前手势
    self.interactivePopGestureRecognizer.enabled = NO;
}

// 决定是否触发手势
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return self.childViewControllers.count > 1;
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    //根控制器不需要返回按钮
    if(self.childViewControllers.count > 0){
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem
                                            backItemWithImage:[UIImage imageNamed:@"navbar_back_normal"]
                                                    highImage:[UIImage imageNamed:@"navbar_back_selected"]
                                                       target:self
                                                       action:@selector(back)
                                                        title:@"返回"];
    }
    
    //真正跳转
    [super pushViewController:viewController animated:YES];
}

-(void)back{
    [self popViewControllerAnimated:YES];
}

@end
